var prefix = ctx + "system/dept";

$(function () {
    initProvince();				//初始化省
    initCity();
    initCounty();
    var options = {
        url: prefix + "/countlist",
        striped: true,
        search:false,	
        showSearch:false,	
        showRefresh:false,	
        showToggle:false,	
        showColumns:false,	
        queryParams: queryParams,
        modalName: "工具器统计",
        columns: [
            {
                title: 'id',
                field: 'id',
                visible: false
            },
            {
                title: "序号",
                formatter: function (value, row, index) {
                    return $.table.serialNumber(index);
                }
            },
            {
            	field: 'deptName',
            	title: '单位名称',
            	sortable: true,
        	    formatter: function (value, item, index) {
                  return '<a  style="cursor:pointer;" onclick="onclickdeptName(\'' + item.deptId + '\',\'' + item.deptNames + '\')">' + $.table.tooltip(value) + '</a>';
 
                }
            },
          
            {
                field: 'zsnums',
                title: '库存总数（件）',
                sortable: true,
                formatter: function (value, item, index) {
                    return value;

                }
            },
            {
            	field: 'zknums',
            	title: '在库总数（件）',
            	sortable: true,
            	formatter: function(value, row, index) {
            		return $.table.tooltip(value);
            	}
            },
            {
            	field: 'dqnum',
            	title: '十天之内到期总数（件）',
            	sortable: true,
            	formatter: function(value, row, index) {
            		return $.table.tooltip(value);
            	}
            },
            {
                field: 'bfb',
                title: '实验合格率（%）',
                sortable: true,
                formatter: function(value, row, index) {
                	return $.table.tooltip(value);
                }
            },
            ]
    };
    $.table.init(options);

});

/**
 * 跳转
 */
function onclickdeptName(dprtcode,deptName) {
	parent.document.getElementById("iframe0").src=ctx +"system/account/"+dprtcode+"?name="+deptName;
}

/**
 * 初始化省
 */
function initProvince() {

    $.ajax({
        cache: true,
        type: "POST",
        url: prefix + "/initProvince",
        async: false,
		data :{parentId:'100'},
        error: function (request) {
            $.modal.alertError("系统错误");
        },
        success: function (data) {
            $('#province').empty();
            var storeHtml = "";
            if($("#dprtcode").val() == "100"){
            	 storeHtml = "<option value=''>显示全部</option>";
            }
            $.each(data, function (index, row) {
            	if(row.deptId == $("#dprtcode").val()){
					
					storeHtml += "<option value=\""+row.deptId+"\" selected=true  >"+row.deptName+"</option>"
				}else{
					
					storeHtml += "<option value=\""+row.deptId+"\"  >"+row.deptName+"</option>"
				}
            })

            
            $("#province").html(storeHtml);
            $("#province").selectpicker('refresh');
            
           
        }
    });
}

/**
 * 变更省
 */
function onchangeprovince(){
	 initCity(); 
	$.table.search();//刷列表
}


/**
 * 初始化市
 */
function initCity() {
	
	$.ajax({
		cache: true,
		type: "POST",
		url: prefix + "/initProvince",
		async: false,
		data :{parentId:$("#province").val()},
		error: function (request) {
			$.modal.alertError("系统错误");
		},
		success: function (data) {
			$('#city').empty();
			if(data.length != 1){
				var storeHtml = "<option value=''>显示全部</option>";
			}
			$.each(data, function (index, row) {
					
					storeHtml += "<option value=\""+row.deptId+"\"  >"+row.deptName+"</option>"
			})
			
			
			$("#city").html(storeHtml);
			if($("#province").val()==""){
				$("#city").html("<option value=''>显示全部</option>");
			}
			$("#city").selectpicker('refresh');
		}
	});
}


/**
 * 变更市
 */
function onchangecity(){
	initCounty(); 
	$.table.search();//刷列表
}


/**
 * 初始化县
 */
function initCounty() {
	
	$.ajax({
		cache: true,
		type: "POST",
		url: prefix + "/initProvince",
		async: false,
		data :{parentId:$("#city").val()},
		error: function (request) {
			$.modal.alertError("系统错误");
		},
		success: function (data) {
			$('#county').empty();
			var storeHtml = "<option value=''>显示全部</option>";
			$.each(data, function (index, row) {
					
					storeHtml += "<option value=\""+row.deptId+"\"  >"+row.deptName+"</option>"
			})
			
			
			$("#county").html(storeHtml);
			if($("#city").val()==""){
				$("#county").html("<option value=''>显示全部</option>");
			}
			$("#county").selectpicker('refresh');
		}
	});
}


/**
 * 变更县
 */
function onchangecounty(){
	parent.document.getElementById("iframe0").src=ctx +"system/main?dprtcode="+$('#county').find("option:selected").attr('value')+"&deptName="+$('#county').find("option:selected").text();
	$.table.search();//刷列表
}


function queryParams(params) {
    return {
    	province:$("#province").val(),
        pageSize: params.limit,
        pageNum: params.offset / params.limit + 1,
        searchValue: params.search,
        orderByColumn: params.sort,
        isAsc: params.order
    };
}
